// 系统相关配置......
import { defineStore } from 'pinia';
import { useParamStore } from './useParamStore';
import { useRefreshStore } from './useRefreshStore'; 

interface ChannelItem {
    c_mobile_module: string;
    c_row_num: number;
}

interface Portal {
    c_type: number;
}
interface ObjectData {
    Portal: Portal;
    ChannelItems: ChannelItem[];
}

interface TabBar {
    title: string;
    sort: number | string;
    pagePath: string;
    icon: string;
    'active_theme-blue': string;
    'active_theme-orangeRed': string;
}

interface MenusAndLimitCodeItem {
    c_menu_code: string;
    c_menu_title: string;
    c_mdcode?: string;
    c_type: string;
    c_md_name?: string;
    c_md_cname?: string;
    c_file?: string;
    c_client_type?: string;
    c_function_type?: string;
    c_origin_type?: string;
    c_status?: string;
    c_version?: string;
    c_limitcode?: string;
    c_ini_param?: string;
    c_menu_extend?: string | null;
    c_menu_img?: string;
    c_system?: string;
    c_start_color?: string;
    c_end_color?: string;
}

interface State {
    homePage: { name: string; sort: number }[];
    dashboard: { name: string; sort: number }[];
    tabBarData: TabBar[];
    MenusAndLimitCode: MenusAndLimitCodeItem[];
};

const tabList = [
    {
        "title": "首页",
        'sort': 1,
        "pagePath": "/packages/MainViews/main/main",
        "icon": "/static/img/tabBar/tabbar_home.png",
        "active_theme-blue": "/static/img/tabBar/tabbar_home_blue.png",
        "active_theme-orangeRed": "/static/img/tabBar/tabbar_home_orangeRed.png"
    },
    {
        "title": "看板",
        'sort': 2,
        "pagePath": "/packages/MainViews/dashboard/index",
        "icon": "/static/img/tabBar/tabbar_kb.png",
        "active_theme-blue": "/static/img/tabBar/tabbar_kb_blue.png",
        "active_theme-orangeRed": "/static/img/tabBar/tabbar_kb_orangeRed.png"

    },
    {
        "title": "任务",
        'sort': 3,
        "pagePath": "/packages/MainViews/taskList/taskList",
        "icon": "/static/img/tabBar/tabbar_quest.png",
        "active_theme-blue": "/static/img/tabBar/tabbar_quest_blue.png",
        "active_theme-orangeRed": "/static/img/tabBar/tabbar_quest_orangeRed.png"
    },
    {
        "title": "应用",
        'sort': 4,
        "pagePath": "/pages/MainViews/menu/menu",
        "icon": "/static/img/tabBar/tabbar_apply.png",
        "active_theme-blue": "/static/img/tabBar/tabbar_apply_blue.png",
        "active_theme-orangeRed": "/static/img/tabBar/tabbar_apply_orangeRed.png"
    },
    {
        "title": "我的",
        'sort': 5,
        "pagePath": "/pages/MainViews/user/user",
        "icon": "/static/img/tabBar/tabbar_me.png",
        "active_theme-blue": "/static/img/tabBar/tabbar_me_blue.png",
        "active_theme-orangeRed": "/static/img/tabBar/tabbar_me_orangeRed.png"
    }
];
export const useModuleDataStore = defineStore('ModuleData', {
    state: (): State => ({
        homePage: [],
        dashboard: [],
        tabBarData: [],
        MenusAndLimitCode: [],
    }),
    actions: {
        setDate(objectData: ObjectData[]) {
            this.dashboard = [];
            this.homePage = [];
            let filterList: TabBar[] = [];
            uni.removeStorageSync('tabBarData');
            // 是否启用任务管理
            const paramStore = useParamStore();
            const { ifShowTaskModule } = paramStore;

            objectData.forEach(data => {
                if (data) {
                    const { Portal, ChannelItems } = data;
                    const sortedItems = ChannelItems.sort((a, b) => a.c_row_num - b.c_row_num);
                    let list:{ name: string; sort: number }[] = [];
    
                    sortedItems.forEach((item) => {
                        list.push({
                            name: item.c_mobile_module,
                            sort: item.c_row_num + 1,
                        });
                    });
                    if (Portal) {
                        const refreshStore = useRefreshStore();
                        if (Number(Portal.c_type) === 1) {
                            this.dashboard = list;
                            refreshStore.triggerRefresh('dashboardRefresh');
                        } else {
                            this.homePage = list;
                            refreshStore.triggerRefresh('HomeRefresh');
                        }
                    }
                }
            });
            filterList = ifShowTaskModule ? tabList : tabList.filter(item => item.title !== "任务");
            this.tabBarData = this.dashboard.length > 0 ? filterList : filterList.filter(item => item.title !== "看板");
            uni.setStorageSync('tabBarData', JSON.stringify(this.tabBarData));
        },
        setMenusAndLimitCode(value: MenusAndLimitCodeItem[]) {
            this.MenusAndLimitCode = value;
        }
    },
});